﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using WordService.Model;
using WordService.Business.IBLL;
using WordService.Business;
using WordService.Utility;
//using System.Collections;


namespace WordLib.MatrixUI
{
    public partial class AuthorPanel : UserControl
    {
        public AuthorPanel()
        {
            InitializeComponent();
        }
        private Boolean _IsRate;
        /// <summary>
        /// 是否带权值
        /// </summary>
        public Boolean IsRate
        {
            get { return _IsRate; }
            set { _IsRate = value; }
        }
        private IList<Author> _OrgAuthorList;
        private DataTable _ExcelTable = new DataTable();
        private IAuthorBLL authorBLL = BLLManager.CreateAuthorBLL();
        /// <summary>
        /// 生成矩阵
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            if (this._OrgAuthorList != null && this._OrgAuthorList.Count > 0)
            {
                _ExcelTable = MatrixUtil.CalculateMatrix(this._OrgAuthorList, this._IsRate);
                this.dgvMatrix.DataSource = _ExcelTable;
                this.dgvMatrix.Update();
            }
            else
            {
                MessageBox.Show(this, "对不起，请先查询，生成线性结果！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        /// <summary>
        /// 重置该表单的筛选条件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReset_Click(object sender, EventArgs e)
        {
            // 重置该表单的筛选条件
            this.txtKeyword.Text = String.Empty;
            this.txtFrequency.Text = "0";
            this.txtInsititution.Text = String.Empty;
            this.txtYear.Text = String.Empty;
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            //if (this._ExcelTable != null && this._ExcelTable.Rows.Count > 0)
            //{
            DialogResult result = this.fbdExportPath.ShowDialog();
            if (result == DialogResult.OK)
            {
                String path = this.fbdExportPath.SelectedPath;
                Microsoft.Office.Interop.Excel.Application
                    xAPP = ExcelUtil.ExportExcel(this._ExcelTable, "作者", ref path);
                MessageBox.Show(this, "您的Excel已经导出成功！其路径为：" + path, "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                // 导出为Excel的表格
            }
            //}
            //else
            // {
            // MessageBox.Show(this, "对不起，请先生成矩阵，再设置导出！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            // }
        }
        /// <summary>
        /// 查询Author的结果
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                Int64 frequency = Convert.ToInt64(this.txtFrequency.Text);
                _OrgAuthorList = this.authorBLL.QueryByCondition(
                    this.txtKeyword.Text,
                    this.txtInsititution.Text,
                    frequency,
                    this.txtYear.Text);
                this.dgvMatrix.DataSource = _OrgAuthorList;
                this.dgvMatrix.Update();
            }
            catch (FormatException)
            {
                MessageBox.Show(this, "对不起，频次信息不能为非数字，请重新输入频次！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }
}
